package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.Cart;
import com.wanmait.computershop.vo.Product;
import com.wanmait.computershop.vo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDAO {

    public static Cart isAdded(Integer uid, Integer pid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from cart where uid=? and pid =?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Cart.class),uid,pid);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


    public static Integer countByUid(Integer uid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT SUM(num) AS cart_product_num FROM cart WHERE uid=?";
        try {
            Object o = queryRunner.query(sql,new ScalarHandler(), uid);
            return Integer.parseInt(o.toString());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public static void delectCartById(Integer cid){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "delete from cart WHERE id = ?";
        try {
            queryRunner.update(sql,cid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void insertByUidAndProduct(Integer uid,Product product,Integer addNum)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into cart (uid,pid,num,price) values (?,?,?,?)";
        try {
            queryRunner.update(sql,uid,product.getId(),addNum,product.getPrice());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static Cart selectById(Integer id)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT *,cart.id cart_id,product.id product_id,cart.num cart_num FROM cart,product WHERE cart.pid = product.id AND product.`enable` = 1 and cart.id=?";
        try {
            Map<String,Object> map =  queryRunner.query(sql,new MapHandler(),id);
            Cart cart = new Cart();
            cart.setId((Integer) map.get("cart_id"));
            cart.setNum((Integer) map.get("cart_num"));
            cart.setPrice((Integer) map.get("price"));
            cart.setPid((Integer) map.get("product_id"));
            Product product = new Product();
            product.setId((Integer) map.get("product_id"));
            product.setPrice((Integer) map.get("price"));
            product.setStocknum((Integer) map.get("stocknum"));
            product.setEnable((Integer) map.get("enable"));
            product.setPriority((Integer) map.get("priority"));
            product.setImage((String) map.get("image"));
            product.setTitle((String) map.get("title"));
            cart.setProduct(product);
            return cart;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void updateNumById(Integer id,Integer num)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "update cart set num=? where id=?";
        try {
            queryRunner.update(sql,num,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static List<Cart> selectAll(Integer uid){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT *,cart.id cart_id,product.id product_id,cart.num cart_num FROM cart,product WHERE cart.pid = product.id AND product.`enable` = 1 and cart.uid = ?";
        try {
            List<Map<String,Object>> mapList = queryRunner.query(sql,new MapListHandler(),uid);
            List<Cart> cartList = new ArrayList<>();
            for(Map map : mapList)
            {
                Cart cart = new Cart();
                cart.setId((Integer) map.get("cart_id"));
                cart.setNum((Integer) map.get("cart_num"));
                cart.setPrice((Integer) map.get("price"));
                cart.setPid((Integer) map.get("product_id"));
                cart.setUid(uid);
                Product product = new Product();
                product.setId((Integer) map.get("product_id"));
                product.setPrice((Integer) map.get("price"));
                product.setStocknum((Integer) map.get("stocknum"));
                product.setEnable((Integer) map.get("enable"));
                product.setPriority((Integer) map.get("priority"));
                product.setImage((String) map.get("image"));
                product.setTitle((String) map.get("title"));
                cart.setProduct(product);
                cartList.add(cart);
            }
            return cartList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


//    public static void main(String[] args) {
//        User user = new User();
//        user.setId(1);
//        List<Cart> cartList = CartDAO.selectAll(user);
//        for(Cart cart : cartList)
//        {
//            System.out.println(cart.getId());
//            System.out.println(cart.getNum());
//            System.out.println(cart.getPrice());
//            System.out.println(cart.getPid());
//            System.out.println(cart.getUid());
//            System.out.println(cart.getProduct().getId());
//            System.out.println(cart.getProduct().getPrice());
//            System.out.println(cart.getProduct().getImage());
//            System.out.println(cart.getProduct().getTitle());
//            System.out.println(cart.getProduct().getNum());
//
//        }
//    }

}
